镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

tls: initialize session and SNI before connecting - #65624

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
styfle:styfle/tls-sync-lookup
Sep 25, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
styfle:styfle/tls-sync-lookup

Conversation

@styfle

@styfle styfle commented Aug 28, 2026

Copy link
Copy Markdown
Member

A synchronous custom lookup callback can cause the connection to fail or abort before tls.connect() applies the TLS session and SNI. The failure clears the TLS handle, causing setServername() to throw a synchronous TypeError.

The original failure surfaced through an HTTP client as:

node:events:497
      throw er; // Unhandled 'error' event
      ^
Error: connect ENETUNREACH 2001:db8::1:443 - Local (:::0)
    at internalConnect (node:net:1111:16)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:472:18)
    at emitLookup (node:net:1524:9)
    at lookup (/app/services-ts/dist/src/request.js:103:33)
    at emitLookup (node:net:1447:5)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:472:18)
    at lookupAndConnectMultiple (node:net:1446:3)
    at node:net:1392:7
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:472:18)
    at lookupAndConnect (node:net:1391:5)
Emitted 'error' event on TLSSocket instance at:
    at emitErrorNT (node:internal/streams/destroy:170:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:89:21) {
  errno: -101,
  code: 'ENETUNREACH',
  syscall: 'connect',
  address: '2001:db8::1',
  port: 443
}

Initialize the session and SNI before starting the underlying TCP connection so the original connection error is emitted normally.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. tls Issues and PRs related to the tls subsystem. labels Aug 28, 2026
@styfle
styfle force-pushed the styfle/tls-sync-lookup branch from 5f0fe2b to 737a73d Compare August 28, 2026 17:30
A synchronous custom lookup can abort the socket before tls.connect()
applies the session and SNI, leaving the TLS handle unavailable.
Initialize both before starting the connection so the original
socket error is emitted normally.

Assisted-by: Codex
Signed-off-by: Steven <steven@ceriously.com>
@styfle
styfle force-pushed the styfle/tls-sync-lookup branch from 737a73d to 24774cc Compare August 28, 2026 17:31
@codecov

codecov Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.04%. Comparing base (0544741) to head (24774cc).
⚠️ Report is 573 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65624      +/-   ##
==========================================
- Coverage   90.07%   90.04%   -0.04%     
==========================================
  Files         751      751              
  Lines      254921   254920       -1     
  Branches    48129    48124       -5     
==========================================
- Hits       229627   229545      -82     
- Misses      16479    16544      +65     
- Partials     8815     8831      +16     
Files with missing lines Coverage Δ
lib/internal/tls/wrap.js 95.17% <100.00%> (-0.01%) ⬇️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 28, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 28, 2026
@nodejs-github-bot

This comment was marked as outdated.

@aduh95 aduh95 added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Aug 28, 2026
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@styfle

styfle commented Aug 30, 2026

Copy link
Copy Markdown
Member Author

I can't see the results of CI - is there a bug in my code or is this a flake?

@nodejs-github-bot

This comment was marked as outdated.

@panva panva added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 3, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 3, 2026
@nodejs-github-bot

This comment has been minimized.

@panva

panva commented Sep 3, 2026

Copy link
Copy Markdown
Member

cc @nodejs/build the build above started by automation does not contain a Resume CI link

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@panva panva added the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 25, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 9827749 into nodejs:main Sep 25, 2026
80 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 9827749

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 25, 2026
@styfle
styfle deleted the styfle/tls-sync-lookup branch September 26, 2026 06:18
aduh95 pushed a commit that referenced this pull request Sep 27, 2026
A synchronous custom lookup can abort the socket before tls.connect()
applies the session and SNI, leaving the TLS handle unavailable.
Initialize both before starting the connection so the original
socket error is emitted normally.

Assisted-by: Codex
Signed-off-by: Steven <steven@ceriously.com>
PR-URL: #65624
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
HoonDongKang pushed a commit to HoonDongKang/node that referenced this pull request Sep 28, 2026
A synchronous custom lookup can abort the socket before tls.connect()
applies the session and SNI, leaving the TLS handle unavailable.
Initialize both before starting the connection so the original
socket error is emitted normally.

Assisted-by: Codex
Signed-off-by: Steven <steven@ceriously.com>
PR-URL: nodejs#65624
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request Sep 28, 2026
A synchronous custom lookup can abort the socket before tls.connect()
applies the session and SNI, leaving the TLS handle unavailable.
Initialize both before starting the connection so the original
socket error is emitted normally.

Assisted-by: Codex
Signed-off-by: Steven <steven@ceriously.com>
PR-URL: #65624
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
thc1006 added a commit to thc1006/sch001-108platform that referenced this pull request Oct 27, 2026
本機跑全站 --record 時整支程式崩潰過一次:Unhandled 'error' event,Error: connect ENETUNREACH
<IPv6 位址>:443。本機沒有 IPv6 的預設路由,connect 對外部 IPv6 位址當下就回 ENETUNREACH。

原因:requestOnce 傳給 https.request 的 lookup 同步呼叫 cb。net 在 lookup 的回呼裡直接連線,
每個位址都同步失敗時(只有一個位址走 internalConnect,多個位址走 internalConnectMultiple),
socket 在 tls.connect 回傳前就被 destroy、_handle 變成 null,tls.connect 接著呼叫
setServername 丟出 TypeError。這個 TypeError 被 requestOnce 的 try/catch 接住,回傳 EREQUEST;
但 socket 已經排好下一個 tick 要發出的 error 事件,https.request 沒有回傳 ClientRequest,沒有人
掛 error 監聽,程式就此結束。只要還有一個位址是非同步失敗或連得上,例如 IPv6 加 IPv4,就不會發生,
所以重跑通常正常。http 不經過 tls.connect,同樣情況會照常回報 req 的 error。

重現(2001:db8::/32 是文件用位址,本機沒有路由):lookup 同步回傳 [2001:db8::1] 或
[2001:db8::1, 2001:db8::2],用 requestOnce 的選項呼叫 https.request,Node 24.21.0 與 24.22.1
都先丟 TypeError: Cannot read properties of null (reading 'setServername'),接著因為
Unhandled 'error' event 結束,單一位址是 ENETUNREACH,兩個位址是 AggregateError [ENETUNREACH]。
[2001:db8::1, 127.0.0.1] 不會。26.12.1 不會。

Node 的修正是 nodejs/node#65624(tls: initialize session and SNI before connecting),
2026-09-25 合併,收在 v26.11.0,v24.x 到 24.22.1 都還沒有。PR 的說明附了同一段錯誤訊息。
nodejs/node#48771 是 http 的同一類問題,nodejs/node#61658(v24.14.0)與 #61770(v24.15.0)
修了 http,tls 這條路徑到 #65624 才修。

修法:lookup 用 setImmediate 回呼,跟 dns.lookup 一樣非同步。連線失敗發生在 tls.connect 回傳、
ClientRequest 掛好 socket 的 error 處理之後,錯誤照一般連線失敗回到 req 的 error。上面的重現改成
非同步回呼之後,三種位址組合在 24.21.0 都回到 req 的 error。

link-health.test.mjs 加 2 條:https 的請求在每個位址都同步連線失敗時,probe 回傳 status 0、
ERR_IP_BLOCKED,不崩潰。同步失敗用 net.BlockList 製造,不看機器有沒有 IPv6 路由;一條只有一個
位址,一條兩個位址走 Happy Eyeballs。對上一個 commit 跑,兩條都失敗(code 是 EREQUEST,並觸發
uncaughtException)。link-health.faults.mjs 的 B 段加 1 項,把 lookup 改回同步回呼,擋下來。

Refs: nodejs/node#65624
Refs: nodejs/node#48771
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. needs-ci PRs that need a full CI run. tls Issues and PRs related to the tls subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants